package com.jhhc.IoUtils;

import java.io.*;
import java.sql.*;

public class App {

    public static void main(String[] args) {

        try {

            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

            // 数据库用户
            String user = "JhhcUser";

            // 数据库密码
            String password = "RiverStrong.522";

            Connection conn = DriverManager.getConnection("jdbc:sqlserver://123.60.35.102:1433;DatabaseName=RHDB_JHHC",
                    user, password);

            Statement stmt = conn.createStatement();

            // 查询 , 从数据库 db_sale 的 product 表中查询 id, name, qty 字段
            ResultSet rs = stmt.executeQuery(
                    "SELECT STCD ,TM ,DRP,INTV,PDR,DYP,WTH FROM ST_PPTN_R WHERE  TM >'2022-03-31 08:00:00'\r\n"
                            + "           and TM <='2022-04-18 08:00:00'");

            // 创建输出文件 result.txt
            File file = new File("d://ST_PPTN_Rx.txt");
            OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file));

            while (rs.next()) {

                writer.write(rs.getString(1) + ",");
                writer.write(rs.getString(2) + ",");
                writer.write(String.valueOf(rs.getInt(3)) + ",");
                writer.write(String.valueOf(rs.getInt(4)) + ",");
                writer.write(String.valueOf(rs.getInt(5)) + ",");
                writer.write(String.valueOf(rs.getInt(6)) + ",");
                writer.write(rs.getString(7) + "");
                writer.write("\r\n");

                //System.out.println(rs.getLong(1));
                //System.out.println(rs.getString(2));
                //System.out.println(rs.getLong(3));
            }
            writer.flush();
            writer.close();
            rs.close();
            //river
            ResultSet rs1 = stmt.executeQuery(
                    "SELECT STCD ,TM ,Z,Q,XSA,XSAVV,XSMXV,FLWCHRCD,WPTN,MSQMT,MSAMT,MSVMT FROM ST_RIVER_R WHERE  TM >'2022-03-31 08:00:00'\r\n"
                            + "           and TM <='2022-04-19 08:00:00'");

            // 创建输出文件 result.txt
            File file1 = new File("d://ST_RIVER_Rx.txt");
            OutputStreamWriter writer1 = new OutputStreamWriter(new FileOutputStream(file1));

            while (rs1.next()) {
                writer1.write(rs1.getString(1) + ",");
                writer1.write(rs1.getString(2) + ",");
                writer1.write(String.valueOf(rs1.getInt(3)) + ",");
                writer1.write(String.valueOf(rs1.getInt(4)) + ",");
                writer1.write(String.valueOf(rs1.getInt(5)) + ",");
                writer1.write(String.valueOf(rs1.getInt(6)) + ",");
                writer1.write(String.valueOf(rs1.getInt(7)) + ",");
                writer1.write(rs1.getString(8) + ",");
                writer1.write(rs1.getString(9) + ",");
                writer1.write(rs1.getString(10) + ",");
                writer1.write(rs1.getString(11) + ",");
                writer1.write(rs1.getString(12) + "");
                writer1.write("\r\n");
            }
            writer1.flush();
            writer1.close();
            rs1.close();

            //RSVR
            ResultSet rs2 = stmt.executeQuery(
                    "SELECT STCD ,TM ,RZ,INQ,W,BLRZ,OTQ,RWCHRCD,RWPTN,INQDR,MSQMT FROM ST_RSVR_R WHERE  TM >'2022-03-31 08:00:00'\r\n"
                            + "           and TM <='2022-04-19 08:00:00'");

            // 创建输出文件 result.txt
            File file2 =  new File("d://ST_RSVR_Rx.txt");
            OutputStreamWriter writer2 = new OutputStreamWriter(new FileOutputStream(file2));

            while (rs2.next()) {
                writer2.write(rs2.getString(1) + ",");
                writer2.write(rs2.getString(2) + ",");
                writer2.write(String.valueOf(rs2.getInt(3)) + ",");
                writer2.write(String.valueOf(rs2.getInt(4)) + ",");
                writer2.write(String.valueOf(rs2.getInt(5)) + ",");
                writer2.write(String.valueOf(rs2.getInt(6)) + ",");
                writer2.write(String.valueOf(rs2.getInt(7)) + ",");
                writer2.write(rs2.getString(8) + ",");
                writer2.write(rs2.getString(9) + ",");
                writer2.write(String.valueOf(rs2.getInt(10)) + ",");
                writer2.write(rs2.getString(11) + "");
                writer2.write("\r\n");
            }
            writer2.flush();
            writer2.close();
            rs2.close();

            stmt.close();
            conn.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
